/*     */ package hmc;
/*     */ 
/*     */ import Serialio.SerialConfig;
/*     */ import Serialio.SerialPortLocal;
/*     */ import java.io.BufferedWriter;
/*     */ import java.io.File;
/*     */ import java.io.FileNotFoundException;
/*     */ import java.io.FileWriter;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintStream;
/*     */ import java.text.SimpleDateFormat;
/*     */ import java.util.Date;
/*     */ import javax.swing.JTextPane;
/*     */ import javax.swing.text.BadLocationException;
/*     */ import javax.swing.text.Document;
/*     */ import javax.swing.text.Position;
/*     */ import javax.swing.text.StyledDocument;
/*     */ import javax.swing.text.html.HTMLEditorKit;
/*     */ 
/*     */ public class SerialPortCommunicator
/*     */ {
/*     */   protected SerialConfig serConfig;
/*  51 */   protected SerialPortLocal serialPort = null;
/*     */   public SerialWriter SW;
/*     */   public SerialReader SR;
/*     */   public Thread SRThread;
/*     */   public TopLevelGUI TLGUI;
/*     */   public Communicator comm;
/*     */   public BufferedWriter fileOutput;
/*     */   public FileWriter fstream;
/*     */   public File logFile;
/*  64 */   public String command = "";
/*     */   public String compareItem;
/*  66 */   public String portName = "";
/*     */   public int baudrate;
/*     */   public int stopbits;
/*     */   public int databits;
/*     */   public int parity;
/*  74 */   boolean toFile = false;
/*     */ 
/*  76 */   int lineNumber = 0;
/*     */ 
/*     */   public SerialPortCommunicator(String portName, TopLevelGUI TLGUI)
/*     */   {
/*  80 */     this.portName = portName;
/*  81 */     this.TLGUI = TLGUI;
/*  82 */     this.baudrate = 9600;
/*  83 */     this.stopbits = 1;
/*  84 */     this.databits = 8;
/*  85 */     this.parity = 0;
/*     */   }
/*     */ 
/*     */   public SerialPortCommunicator(String portName, TopLevelGUI TLGUI, int BR, int DB, int SB, int P) {
/*  89 */     this.portName = portName;
/*  90 */     this.TLGUI = TLGUI;
/*  91 */     this.baudrate = BR;
/*  92 */     this.stopbits = SB;
/*  93 */     this.databits = DB;
/*  94 */     this.parity = P;
/*     */   }
/*     */ 
/*     */   public void sendCommand(String x) {
/*  98 */     this.command = (x + "\r");
/*     */     try
/*     */     {
/* 101 */       this.serialPort.txDrain();
/* 102 */       this.serialPort.rxFlush();
/* 103 */       this.SW.send();
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/*     */     }
/*     */ 
/* 109 */     this.command = "";
/*     */   }
/*     */ 
/*     */   public void simpleSendCommand(String x) {
/* 113 */     this.command = x.replace("\n", "\r");
/*     */     try
/*     */     {
/* 116 */       this.serialPort.txDrain();
/* 117 */       this.serialPort.rxFlush();
/* 118 */       this.SW.send();
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/*     */     }
/*     */ 
/* 124 */     this.command = "";
/*     */   }
/*     */ 
/*     */   public void closeStreams()
/*     */   {
/*     */     try {
/* 130 */       if (this.SR.isConnected())
/* 131 */         this.comm.JavaPrinting("\nDisconnected.\n");
/* 132 */       this.SW.closeStream();
/* 133 */       this.SR.closeStream();
/* 134 */       this.serialPort.close();
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/*     */     }
/*     */   }
/*     */ 
/*     */   public void connect()
/*     */   {
/*     */     try
/*     */     {
/* 145 */       System.out.println("SERIAL PORT COMMUNICATOR  " + this.portName);
/* 146 */       this.serConfig = new SerialConfig(this.portName);
/* 147 */       this.serialPort = new SerialPortLocal(this.serConfig);
/*     */ 
/* 149 */       this.SR = new SerialReader(this.serialPort, this.comm);
/* 150 */       this.SRThread = new Thread(this.SR);
/*     */ 
/* 152 */       this.SRThread.start();
/*     */ 
/* 154 */       this.SW = new SerialWriter(this.serialPort);
/* 155 */       new Thread(this.SW).start();
/*     */ 
/* 157 */       this.comm.JavaPrinting(" \nConnected To: " + this.portName + " \n \n");
/* 158 */       this.comm.ConsoleBox.setCaretPosition(this.comm.ConsoleBox.getDocument().getLength());
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/* 170 */       e.printStackTrace();
/*     */     }
/*     */   }
/*     */ 
/*     */   public void setSPC(Communicator x) {
/* 175 */     this.comm = x;
/*     */   }
/*     */ 
/*     */   public void writeLogFile(File logFile, boolean HTMLOutput, int dataLogStartLine)
/*     */   {
/*     */     try {
/* 181 */       this.fstream = new FileWriter(logFile);
/* 182 */       this.fileOutput = new BufferedWriter(this.fstream);
/* 183 */       SimpleDateFormat custDateFormat = new SimpleDateFormat("EEE MMM-dd HH:mm:ss.SSS zzz yyyy");
/* 184 */       Date date = new Date();
/* 185 */       this.fileOutput.write("[" + custDateFormat.format(date) + "]\n");
/*     */ 
/* 187 */       if (!HTMLOutput)
/*     */       {
/* 189 */         String text = this.comm.ConsoleBox.getText();
/* 190 */         String[] temp = text.split("\n");
/*     */ 
/* 192 */         for (int x = dataLogStartLine; x < temp.length; x++)
/*     */         {
/* 194 */           this.fileOutput.write(text);
/*     */         }
/*     */       }
/*     */       else
/*     */       {
/* 199 */         StyledDocument doc = this.comm.ConsoleBox.getStyledDocument();
/*     */ 
/* 201 */         HTMLEditorKit kit = new HTMLEditorKit();
/*     */         try
/*     */         {
/* 205 */           kit.write(this.fileOutput, doc, doc.getStartPosition().getOffset(), doc.getLength());
/*     */         }
/*     */         catch (FileNotFoundException e)
/*     */         {
/*     */         }
/*     */         catch (IOException e)
/*     */         {
/*     */         }
/*     */         catch (BadLocationException e)
/*     */         {
/*     */         }
/*     */ 
/*     */       }
/*     */ 
/* 221 */       this.fileOutput.close();
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/* 225 */       e.printStackTrace();
/*     */     }
/*     */   }
/*     */ 
/*     */   public class SerialWriter
/*     */     implements Runnable
/*     */   {
/*     */     SerialPortLocal serialPort;
/*     */ 
/*     */     public SerialWriter(SerialPortLocal out)
/*     */     {
/* 339 */       this.serialPort = out;
/*     */     }
/*     */ 
/*     */     public void run()
/*     */     {
/*     */     }
/*     */ 
/*     */     public void send() {
/* 347 */       byte[] c = SerialPortCommunicator.this.command.getBytes();
/*     */       try
/*     */       {
/* 351 */         this.serialPort.putData(c);
/*     */       }
/*     */       catch (Exception e)
/*     */       {
/*     */       }
/*     */     }
/*     */ 
/*     */     public void closeStream()
/*     */     {
/*     */       try
/*     */       {
/* 362 */         if (SerialPortCommunicator.this.fileOutput != null) {
/* 363 */           SerialPortCommunicator.this.fileOutput.close();
/*     */         }
/* 365 */         this.serialPort.close();
/*     */       }
/*     */       catch (Exception e)
/*     */       {
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public class SerialReader
/*     */     implements Runnable
/*     */   {
/* 230 */     public boolean running = true;
/*     */     Communicator comm;
/*     */     public SerialPortLocal serialPort;
/*     */ 
/*     */     public SerialReader(SerialPortLocal in, Communicator x)
/*     */     {
/* 236 */       this.serialPort = in;
/* 237 */       this.comm = x;
/*     */     }
/*     */ 
/*     */     public void run() {
/* 241 */       while (this.running)
/*     */       {
/*     */         try
/*     */         {
/* 245 */           while (this.serialPort.rxReadyCount() > 0)
/*     */           {
/* 247 */             boolean backspaced = false;
/* 248 */             int len = 0;
/*     */ 
/* 251 */             byte[] da = new byte[2056];
/*     */ 
/* 253 */             int data = this.serialPort.getData(da);
/*     */ 
/* 255 */             if (data > 0)
/*     */             {
/* 257 */               String dat = new String(da, 0, data);
/*     */ 
/* 259 */               dat.replace("\b", "*b");
/*     */ 
/* 278 */               if (!backspaced)
/*     */               {
/* 280 */                 int pos = this.comm.ConsoleBox.getDocument().getLength();
/*     */ 
/* 282 */                 if (!dat.equals("")) {
/* 283 */                   this.comm.append(dat);
/*     */                 }
/* 285 */                 if (pos != this.comm.ConsoleBox.getDocument().getLength())
/* 286 */                   this.comm.ConsoleBox.setCaretPosition(this.comm.ConsoleBox.getDocument().getLength());
/*     */               }
/* 288 */               backspaced = false;
/*     */             }
/*     */ 
/* 303 */             Thread.yield();
/*     */           }
/*     */ 
/* 307 */           Thread.sleep(10L);
/*     */         }
/*     */         catch (Exception e)
/*     */         {
/* 311 */           System.out.println("serial read failed");
/* 312 */           e.printStackTrace();
/*     */         }
/*     */       }
/*     */     }
/*     */ 
/*     */     public void closeStream()
/*     */     {
/*     */       try {
/* 320 */         this.running = false;
/* 321 */         this.serialPort.close();
/*     */       }
/*     */       catch (Exception e)
/*     */       {
/*     */       }
/*     */     }
/*     */ 
/*     */     public boolean isConnected()
/*     */     {
/* 330 */       return this.running;
/*     */     }
/*     */   }
/*     */ }

/* Location:           F:\6-30-2012\Hazell\
 * Qualified Name:     hmc.SerialPortCommunicator
 * JD-Core Version:    0.6.0
 */